package org.marsdonne.commons;

import lombok.Data;

import java.util.Calendar;
import java.util.Date;

public final class DateRange {

    private Date startWith;
    private Date endWith;

    private DateRange(Date startWith, Date endWith) {
        this.startWith = startWith;
        this.endWith = endWith;
    }

    public Date getStartWith() {
        return startWith;
    }

    public Date getEndWith() {
        return endWith;
    }

    public static DateRange getDateRangeByCalendarUnitWhenInvalid(Date startWith, Date endWith) {
        return getDateRangeByCalendarUnitWhenInvalid(startWith, endWith, Calendar.DAY_OF_MONTH);
    }

    public static DateRange getDateRangeByCalendarUnitWhenInvalid(Date startWith, Date endWith, int calendarUnit) {
        Calendar calendar = Calendar.getInstance();

        if (endWith == null && startWith == null) {
            endWith = calendar.getTime();
            calendar.add(calendarUnit, 1);
            startWith = calendar.getTime();
        } else if (endWith == null) {
            calendar.setTime(startWith);
            calendar.add(calendarUnit, 1);
            endWith = calendar.getTime();
        } else if (startWith == null) {
            calendar.setTime(endWith);
            calendar.add(calendarUnit, -1);
            startWith = calendar.getTime();
        }

        return new DateRange(startWith, endWith);
    }
}
